package com.ruoyi.base.service.impl;

import com.ruoyi.base.domain.BaseAlternateAirport;
import com.ruoyi.base.mapper.BaseAlternateAirportMapper;
import com.ruoyi.base.service.IBaseAlternateAirportService;
import com.ruoyi.common.exception.ServiceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

/**
 * 备降机场信息;Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-07-22
 */
@Service
public class BaseAlternateAirportServiceImpl implements IBaseAlternateAirportService 
{

    private static final Logger log = LoggerFactory.getLogger(BaseAlternateAirportServiceImpl.class);
    @Autowired
    private BaseAlternateAirportMapper baseAlternateAirportMapper;

    /**
     * 查询备降机场信息;
     * 
     * @param ID 备降机场信息;主键
     * @return 备降机场信息;
     */
    @Override
    public BaseAlternateAirport selectBaseAlternateAirportByID(String ID)
    {
        return baseAlternateAirportMapper.selectBaseAlternateAirportByID(ID);
    }

    /**
     * 查询备降机场信息;列表
     * 
     * @param baseAlternateAirport 备降机场信息;
     * @return 备降机场信息;
     */
    @Override
    public List<BaseAlternateAirport> selectBaseAlternateAirportList(BaseAlternateAirport baseAlternateAirport)
    {
        return baseAlternateAirportMapper.selectBaseAlternateAirportList(baseAlternateAirport);
    }

    /**
     * 新增备降机场信息;
     * 
     * @param baseAlternateAirport 备降机场信息;
     * @return 结果
     */
    @Override
    public int insertBaseAlternateAirport(BaseAlternateAirport baseAlternateAirport)
    {
        return baseAlternateAirportMapper.insertBaseAlternateAirport(baseAlternateAirport);
    }

    /**
     * 修改备降机场信息;
     * 
     * @param baseAlternateAirport 备降机场信息;
     * @return 结果
     */
    @Override
    public int updateBaseAlternateAirport(BaseAlternateAirport baseAlternateAirport)
    {
        return baseAlternateAirportMapper.updateBaseAlternateAirport(baseAlternateAirport);
    }

    /**
     * 批量删除备降机场信息;
     * 
     * @param IDs 需要删除的备降机场信息;主键
     * @return 结果
     */
    @Override
    public int deleteBaseAlternateAirportByIDs(String[] IDs)
    {
        return baseAlternateAirportMapper.deleteBaseAlternateAirportByIDs(IDs);
    }

    /**
     * 删除备降机场信息;信息
     * 
     * @param ID 备降机场信息;主键
     * @return 结果
     */
    @Override
    public int deleteBaseAlternateAirportByID(String ID)
    {
        return baseAlternateAirportMapper.deleteBaseAlternateAirportByID(ID);
    }

    @Override
    public String importMainAirport(List<BaseAlternateAirport> airportList, boolean updateSupport, String operId) {
        if (Objects.isNull(airportList) || airportList.isEmpty()) {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (BaseAlternateAirport airport : airportList) {
            try {
                // 验证是否存在这个机场
                BaseAlternateAirport dbAirport = baseAlternateAirportMapper.selectBaseAlternateAirportByCode(airport.getCode());
                if (Objects.isNull(dbAirport)) {
                    airport.setID(UUID.randomUUID().toString().replace("-", ""));
                    airport.setCreatedBy(operId);
                    airport.setCreatedTime(new Date());
                    baseAlternateAirportMapper.insertBaseAlternateAirport(airport);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、备降机场：" + airport.getName() + " 导入成功");
                } else if (updateSupport) {
                    airport.setID(dbAirport.getID());
                    airport.setUpdatedBy(operId);
                    airport.setUpdatedTime(new Date());
                    baseAlternateAirportMapper.updateBaseAlternateAirport(airport);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、备降机场：" + airport.getName() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、备降机场：" + airport.getName() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、备降机场：" + airport.getName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
